package band.wukong.practice.netty;

import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import org.apache.logging.log4j.util.Strings;

import javax.net.ssl.SSLException;

/**
 * Desc:
 * author: wukonggg@139.com
 */
public class Env {

    public static final String ENCODE = "UTF-8";
    public static final String HOST = "localhost";
    public static final int PORT = 8080;

    /**
     * 裝配log4j2的配置文件
     */
    public static void assemblingLog4j2ConfFile() {
        final String key = "log4j.configurationFile";

        String val = System.getProperty(key);
        if (null !=  val && !val.isEmpty()) {
            System.out.println("[WK-APP-WARN] " + key + "当前值为{" + val + "}，即将被覆盖！");
        }
        System.setProperty(key, "etc/log4j2.xml");
    }

    /**
     * 裝配SSL
     * @param ssl
     */
    public static SslContext assemblingSSL(boolean ssl) {
        SslContext sslCtx = null;
        if (ssl) {
            try {
                sslCtx = SslContextBuilder.forClient()
                        .trustManager(InsecureTrustManagerFactory.INSTANCE).build();
            } catch (SSLException e) {
                e.printStackTrace();
                throw new RuntimeException("SSL构建失败");
            }
        } else {
            sslCtx = null;
        }
        return sslCtx;
    }

}
